<?php

namespace App\Http\Controllers;

use App\Models\GotoList;
use App\Models\Pages;
use App\Models\Site;
use App\Models\Area;
use App\Models\Tag;
use App\Models\Article;
use App\Models\Column;
use App\Models\Link;

class PagesController extends Controller
{
    //
    public function index()
    {
        $is_best    = Site::where('is_best', 1)->where('state', 1)->take(6)->get();
        $newest     = Site::where('state', 1)->orderBy('created_at', 'desc')->take(20)->get();
        $nav_area   = Area::take(10)->get();
        $count      = Tag::count();
        $tag_num    = intval($count/25);
        $tags       = Tag::skip(mt_rand(0,$tag_num))->take(24)->get();
        $col        = Column::where('type',0)->where('parent_id',0)->get();
        $col_art    = Column::where('type',1)->get();
        $col_array  = Column::where('type',1)->where('state','like','%"1"%')->where('state','like','%"3"%')->get()->toArray();
        $articles   = Article::whereIn('column_id',$col_array)->orderBy('updated_at', 'desc')->take(21)->get();
        $links  = Link::where('state',1)->get();
        return view('welcome',compact('links','col_art','articles','tags','nav_area','col','is_best','newest'));
    }
    public function pages($name)
    {
        $pages  = Pages::get();
        $page  = Pages::where('name', $name)->first();
        return view('pages', compact('page', 'pages', 'name'));
    }
    public function goTos($name)
    {
        $first  = GotoList::where('name',$name)->first();
        if($first) {
            return redirect($first->url);
        }else{
            return redirect('/');
        }
    }
}
